package com.cvit.controller;

import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.cvit.model.SchoolMajor;
import com.cvit.service.SchoolMajorService;
import com.cvit.utils.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class SchoolMajorController {

	@Autowired
	SchoolMajorService schoolMajorService;
	
	/**
	 * 查询所有
	 * */
	@RequestMapping("/getSchoolMajor")
	@ResponseBody
	public Msg getAchievements(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
		PageHelper.startPage(pn, 10);
		List<SchoolMajor> schoolMajor = schoolMajorService.getAll();
		PageInfo<SchoolMajor> page = new PageInfo<SchoolMajor>(schoolMajor, 10);
		return Msg.success().add("pageInfo", page);
	}
	
	/**
	 * 查询学校
	 * */
	@RequestMapping("/selSchool")
	@ResponseBody
	public Msg selSchool(){
		List<SchoolMajor> schoolMajor = schoolMajorService.selschool();
		return Msg.success().add("schools", schoolMajor);
	}
	
	/**
	 * 查询专业
	 * */
	@RequestMapping("/selMajor")
	@ResponseBody
	public Msg selMajor(@RequestParam(value = "school", defaultValue = "") String school){
		List<SchoolMajor> schoolMajor = schoolMajorService.selmajor(school);
		return Msg.success().add("majors", schoolMajor);
	}
	
	/**
	 * 导入Excel
	 * */
	@RequestMapping("/SchoolMajorImport")
	@ResponseBody
	public Msg impotr(HttpServletRequest request, Model model) throws Exception {
	     //获取上传的文件 
	     MultipartHttpServletRequest multipart = (MultipartHttpServletRequest) request;
	     MultipartFile file = multipart.getFile("upfile");
	     InputStream in = file.getInputStream();
	     //数据导入  
	     schoolMajorService.importExcelInfo(in,file);
	     in.close();
//	     return Msg.success();
	     return null;
	}
}
